﻿#include "ProjectWindows.h"

#include "Widgets/EditorCommonWindow.h"
#include "Widgets/EditableTextWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/ButtonWidget.h"
#include "Widgets/WindowManager.h"
#include "Widgets/Windows/MessageBox.h"
#include "Widgets/Windows/FileDialog.h"
#include "Project/ProjectModel.h"

#include "Widgets/EditorImageButton.h"
#include "Widgets/EditorTextButton.h"
#include "Widgets/EditorEditableTextWidget.h"

#include "Asset/EditorDefaultPackage.h"

#include "Framework/Editor.h"


void WHE::OpenProjectCreateWindow()
{
    //create a window to get project name and path
    WHE::SCommonWindowCreateInfo WindowInfo;
    WindowInfo.Title=U"创建项目";
    WindowInfo.Size=SIntPoint(500,210);
    WindowInfo.MinimizeMaximizeButton=false;
    WindowInfo.Resizeable=false;
    WindowInfo.bTitleBlinkEffectForModalWindow=true;

    auto Result=WHE::CreateCommonWindow(WindowInfo);

    //create widgets
    auto Window=Result.Window;
    auto WindowContent=Result.WindowContent;
    Window->MoveToScreenCenter();
    RWindowWeakPtr WindowWeak=Window;

    float FirstLineTopY=10;
    float SecondLineTopY=FirstLineTopY+40;
    float ThirdLineTopY=SecondLineTopY+40;
    float FourthLineTopY=ThirdLineTopY+40;
    float HorizontalPadding=10;
    float LineHeight=30;

    REditableTextWidgetWeakPtr NameWidget;
    REditableTextWidgetWeakPtr PathWidget;

    //project name line
    {
        auto ProjectNameText=NewWidget<RTextWidget>(WindowContent);
        ProjectNameText->Text=U"项目名称";
        ProjectNameText->Width=80;
        ProjectNameText->X=HorizontalPadding;
        ProjectNameText->Y=FirstLineTopY;
        ProjectNameText->Height=LineHeight;

        auto EditTextResult=WHE::CreateEditableTextWidget(WindowContent);
        auto ProjectNameEditBackground=EditTextResult.Widget;
        ProjectNameEditBackground->Y=FirstLineTopY;
        ProjectNameEditBackground->Anchor.RightReference=WindowContent;
        ProjectNameEditBackground->Anchor.RightOffset=HorizontalPadding;
        ProjectNameEditBackground->Anchor.LeftReference=ProjectNameText;
        ProjectNameEditBackground->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
        ProjectNameEditBackground->Anchor.LeftOffset=5;

        NameWidget=EditTextResult.TextWidget;
        EditTextResult.TextWidget->UpdateTextOnAccepted=true;
        EditTextResult.TextWidget->UpdateTextOnLostFocus=true;
    }

    //project path line
    {
        auto ProjectPathText=NewWidget<RTextWidget>(WindowContent);
        ProjectPathText->Text=U"项目路径";
        ProjectPathText->Width=80;
        ProjectPathText->X=HorizontalPadding;
        ProjectPathText->Y=SecondLineTopY;
        ProjectPathText->Height=LineHeight;

        auto EditableTextResult=WHE::CreateEditableTextWidget(WindowContent);
        auto ProjectPathEditBackground=EditableTextResult.Widget;
        EditableTextResult.TextWidget->UpdateTextOnAccepted=true;
        EditableTextResult.TextWidget->UpdateTextOnLostFocus=true;
        auto SelectPathButton=WHE::CreateImageButton(
            REditorDefaultPackage::Get()->GetIcon(U"CodiconEllipsis"),
            CString(U"打开文件浏览器以选择路径"),
            [&PathWidget]()
            {
                auto ProjectPath=WH::CreateFolderDialog(U"选择项目路径");
                if(ProjectPath.HasValue())
                {
                    //PathWidget->ForceStopEdit();
                    PathWidget->Text= ProjectPath.GetValue().ToString();
                }
            },
            WindowContent
        );

        ProjectPathEditBackground->Y=SecondLineTopY;
        ProjectPathEditBackground->Height=LineHeight;
        ProjectPathEditBackground->Anchor.RightReference=SelectPathButton;
        ProjectPathEditBackground->Anchor.RightReferenceEdge=EHorizontalWidgetEdge::Left;
        ProjectPathEditBackground->Anchor.RightOffset=5;
        ProjectPathEditBackground->Anchor.LeftReference=ProjectPathText;
        ProjectPathEditBackground->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
        ProjectPathEditBackground->Anchor.LeftOffset=5;

        PathWidget= EditableTextResult.TextWidget;

        SelectPathButton->Anchor.RightReference=WindowContent;
        SelectPathButton->Anchor.RightOffset=HorizontalPadding;
        //height is already set
        SelectPathButton->Anchor.BottomReference=PathWidget;
        
    }

    //error message line
    CString ErrorMessage;
    {
        auto ErrorMessageText=NewWidget<RTextWidget>(WindowContent);
        ErrorMessageText->Text=[&ErrorMessage]()->CString
        {
            return ErrorMessage;
        };
        ErrorMessageText->X=HorizontalPadding;
        ErrorMessageText->Y=ThirdLineTopY;
        ErrorMessageText->Height=LineHeight;
        ErrorMessageText->Color=SColor::FromHexRGB(0xEE0000);
        ErrorMessageText->FontSize=14;
    }

    //create button
    bool bProjectCreated=false;
    SPath FinalProjectPath;
    {
        auto CreateButton = WHE::CreateTextButton(U"创建"
            , [NameWidget
            ,PathWidget
            ,WindowWeak
            ,&ErrorMessage
            ,&bProjectCreated
            ,&FinalProjectPath
            ]() 
        {
            CString ProjectName=NameWidget->Text.Get();
            CString ProjectPath=PathWidget->Text.Get();
            auto OptionalErrorMessage= 
                REditor::Get()->GetSubsystem<RProjectModel>()
                ->CreateProject(ProjectName,ProjectPath);
            if(!OptionalErrorMessage.HasValue())
            {
                bProjectCreated=true;
                FinalProjectPath=ProjectPath;
                WindowWeak->Close();
            }
            else
            {
                ErrorMessage=U"错误: "+OptionalErrorMessage.GetValue();
            }
        }
        ,WindowContent);
        
        CreateButton->Width=80;
        CreateButton->Anchor.RightReference=WindowContent;
        CreateButton->Anchor.RightOffset=HorizontalPadding;
        CreateButton->Height=LineHeight;
        CreateButton->Y=FourthLineTopY;

    }

    Window->MoveToScreenCenter();
    //show window
    Window->Show();

    RWindowManager::Get()->MakeWindowModal(Window);

    if(bProjectCreated)
    {
        WHE::OpenProjectWithErrorMessageBox(FinalProjectPath);
    }
}
void WHE::OpenProjectWithErrorMessageBox(const SPath &ProjectFolderPath)
{
    auto Error=
        REditor::Get()->GetSubsystem<RProjectModel>()
        ->OpenProject(ProjectFolderPath);
    if(Error.HasValue())
    {
        WH::ShowMessageBox(U"错误",Error.GetValue());
    }
}

void WHE::ImportExistProjectWithFileDialog()
{
    auto ProjectPath=WH::CreateFolderDialog(U"选择项目路径");
    if(ProjectPath.HasValue())
    {
        WHE::OpenProjectWithErrorMessageBox(ProjectPath.GetValue());
    }

}